package com.app.teaware.carstack;

import android.content.Context;
import android.graphics.PorterDuff;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.core.content.ContextCompat;

import com.app.teaware.R;
import com.app.teaware.bean.LoginBean;
import com.app.teaware.bean.UserListData;
import com.app.teaware.home.HomeTabActivity;
import com.loopeer.cardstack.CardStackView;
import com.loopeer.cardstack.StackAdapter;

import java.util.Random;

import my.ActivityTool;
import my.MathUtils;

public class TestStackAdapter extends StackAdapter<UserListData> {


    public TestStackAdapter(Context context) {
        super(context);
    }


    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void bindView(UserListData data, int position, CardStackView.ViewHolder holder) {
        if (holder instanceof ColorItemLargeHeaderViewHolder) {
            ColorItemLargeHeaderViewHolder h = (ColorItemLargeHeaderViewHolder) holder;
            h.onBind(data, position);
        }
    }

    @Override
    protected CardStackView.ViewHolder onCreateView(ViewGroup parent, int viewType) {
        View view;
        view = getLayoutInflater().inflate(R.layout.list_card_item, parent, false);
        return new ColorItemLargeHeaderViewHolder(view);

    }

    @Override
    public int getItemViewType(int position) {
            return R.layout.list_card_item;
    }




    static class ColorItemLargeHeaderViewHolder extends CardStackView.ViewHolder {
        View mLayout;
        TextView mTextTitle;
        TextView itemView;
        public  Integer[] colors = new Integer[]{
                R.mipmap.mp_icon1,
                R.mipmap.mp_icon2,
                R.mipmap.mp_icon3,
                R.mipmap.mp_icon4,
                R.mipmap.mp_icon5,
                R.mipmap.mp_icon6,
                R.mipmap.mp_icon7,
                R.mipmap.mp_icon8,
                R.mipmap.mp_icon9,
                R.mipmap.mp_icon10,
                R.mipmap.mp_icon11,
                R.mipmap.mp_icon12,
                R.mipmap.mp_icon13
        };

        public ColorItemLargeHeaderViewHolder(View view) {
            super(view);
            mLayout = view.findViewById(R.id.frame_list_card_item);
            mTextTitle = (TextView) view.findViewById(R.id.text_list_card_title);
            itemView = (TextView) view.findViewById(R.id.btn_ok);
        }

        @Override
        public void onItemExpand(boolean b) {
            itemView.setVisibility(b ? View.VISIBLE : View.GONE);
        }

        @Override
        protected void onAnimationStateChange(int state, boolean willBeSelect) {
            super.onAnimationStateChange(state, willBeSelect);
            if (state == CardStackView.ANIMATION_STATE_START && willBeSelect) {
                onItemExpand(true);
            }
            if (state == CardStackView.ANIMATION_STATE_END && !willBeSelect) {
                onItemExpand(false);
            }
        }

        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        public void onBind(UserListData data, int position) {
//            mLayout.getBackground().setColorFilter(ContextCompat.getColor(getContext(), colors[MathUtils.random(colors.length)]), PorterDuff.Mode.SRC_IN);
            mLayout.setBackground(getContext().getDrawable(colors[MathUtils.random(colors.length)]));
            mTextTitle.setText(data.getAname());
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    LoginBean loginBean = LoginBean.getInstance();
                    loginBean.setAcode(data.getAcode());
                    loginBean.setAname(data.getAname());
                    loginBean.save();
                    ActivityTool.skipActivity(getContext(), HomeTabActivity.class);
                }
            });
        }

    }

}
